package cc.wanforme.fsync.client;

import cc.wanforme.fsync.entity.device.DeviceInfo;
import cc.wanforme.fsync.entity.device.HeartBeat;
import cc.wanforme.st.server.util.FileUtil;
import cc.wanforme.fsync.util.LoggerTool;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class ClientCenter {
    private static final Logger log = LoggerTool.getLogger();

//    private ClientProperty clientProperty;
//
//    private HttpTool httpTool;


    /** 组装心跳消息 */
    public HeartBeat heartBeat() throws IOException {
        List<DeviceInfo> deviceInfos = DeviceInfo.systemInfo();
        HeartBeat heartBeat = new HeartBeat();
        heartBeat.setDeviceInfos(deviceInfos);
        return heartBeat;
    }

    /** 保存文件 */
    public boolean saveFile(String path, MultipartFile file, Long modifyTime, boolean cover, HttpServletRequest request)
            throws IOException {
        log.info("sync-file1:  " + path);
        File f = new File(path);
//        if(f.exists() && !cover) {
//            return false;
//        }

        // 不覆盖，保留原来的旧文件。对旧文件重命名
        if(!cover) {
            String bakFileName = FileUtil.checkAndRenameFile(path);
            File bakFile = new File(bakFileName);
            // 旧文件重命名
            f.renameTo(bakFile);
        }

        try {
            FileUtil.saveFile(file, f);
            if(modifyTime != null && modifyTime.longValue() > 0) {
                f.setLastModified(modifyTime.longValue());
            }
        } catch (IOException e) {
            log.error("Error while saving", e);
            throw e;
        }
        return true;
    }

    /** 本地文件复制到本地其它地方 */
    public static boolean copyLocal(String sourcePath, String targetPath)  throws IOException {
        File source = new File(sourcePath);
        if( !source.exists() ) {
            return false;
        }

        if(source.isFile()) {
            FileUtil.copyFile(source, targetPath);
            return true;
        }

        File[] files = source.listFiles();
        if(files==null || files.length==0) {
            return true;
        }

        for (File f : files) {
            String childSource = f.getPath();
            File childTFile = new File(targetPath, f.getName());
            String childTarget = childTFile.getPath();
            copyLocal(childSource, childTarget);

            childTFile.setLastModified(f.lastModified());
            if(f.isFile()) {
                childTFile.setReadable(f.canRead());
                childTFile.setWritable(f.canWrite());
                childTFile.setExecutable(f.canExecute());
            }
        }
        return true;
    }

    /** 删除目录或文件 */
    public boolean deleteFileOrFolder(String path) {
        File file = new File(path);
        return cn.hutool.core.io.FileUtil.del(file);
//        return file.delete();
    }


//    @Autowired
//    public void setClientProperty(ClientProperty clientProperty) {
//        this.clientProperty = clientProperty;
//    }
//
//    @Autowired
//    public void setHttpTool(HttpTool httpTool) {
//        this.httpTool = httpTool;
//    }

}
